<template>
  <el-dialog
      title="添加/修改设备"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="设备ID" prop="maintenanceRecord.applicant" size="small">
        <el-input size="small" v-model="maintenanceRecord.applicant"></el-input>
      </el-form-item>
      <el-form-item label="申请时间" prop="maintenanceRecord.applicationTime" size="small">
        <el-input size="small" v-model="maintenanceRecord.applicationTime"></el-input>
      </el-form-item>
      <el-form-item label="申请人" prop="maintenanceRecord.classroomCode" size="small">
        <el-input size="small" v-model="maintenanceRecord.classroomCode"></el-input>
      </el-form-item>
      <el-form-item label="故障描述" prop="maintenanceRecord.faultDescription" size="small">
        <el-input size="small" v-model="maintenanceRecord.faultDescription"></el-input>
      </el-form-item>
      <el-form-item label="维修人员" prop="maintenanceRecord.maintenancePeople" size="small">
        <el-input size="small" v-model="maintenanceRecord.maintenancePeople"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="maintenanceRecord.state" size="small">
        <el-select size="small" v-model="maintenanceRecord.state">
          <el-option
              v-for="item in state"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改设备</el-button>
        <el-button type="primary" size="small" plain @click="onSubmit" v-else>添加设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getMaintenanceRecordById, updateMaintenanceRecord, createMaintenanceRecord} from "@/api/maintenanceRecord";

export default {
  name: 'MaintenanceEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    },

  },
  data() {
    return {
      maintenanceRecord: {},
      state:[
        {id: 1, name: '未受理'},
        {id: 2, name: '已受理'},
        {id: 3, name: '已维修'},
      ],
    }
  },
  methods: {

    // 添加设备信息
    onSubmit() {
      createMaintenanceRecord({
        'applicant': this.maintenanceRecord.applicant,
        'applicationTime': this.maintenanceRecord.applicationTime,
        'classroomCode': this.maintenanceRecord.classroomCode,
        'createTime	': this.maintenanceRecord.createTime	,
        'faultDescription': this.maintenanceRecord.faultDescription,
        'id': this.maintenanceRecord.id,
        'maintenancePeople': this.maintenanceRecord.maintenancePeople,
        'maintenanceResult': this.maintenanceRecord.maintenanceResult,
        'state': this.maintenanceRecord.state,
        'username': this.maintenanceRecord.username,

      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新设备信息
    handleUpdate() {
      updateMaintenanceRecord({
        'applicant': this.maintenanceRecord.applicant,
        'applicationTime': this.maintenanceRecord.applicationTime,
        'classroomCode': this.maintenanceRecord.classroomCode,
        'createTime	': this.maintenanceRecord.createTime	,
        'faultDescription': this.maintenanceRecord.faultDescription,
        'id': this.maintenanceRecord.id,
        'maintenancePeople': this.maintenanceRecord.maintenancePeople,
        'maintenanceResult': this.maintenanceRecord.maintenanceResult,
        'state': this.maintenanceRecord.state,
        'username': this.maintenanceRecord.username,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getMaintenanceRecordById(this.id).then((response) => {
          this.maintenanceRecord = response.data.data;
          delete this.maintenanceRecord.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.maintenanceRecord = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
